package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetIhomeBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/25.
 */
public class ProfessionInfoAdapter extends MyAdapter<GetIhomeBean.DataEntity> {

    public ProfessionInfoAdapter(Context context, List<GetIhomeBean.DataEntity> list) {
        super(context, list);
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Integer.parseInt(list.get(position).getYs());
        switch (type) {
            case 1:
                return 0;
            case 2:
                return 1;
            case 3:
                return 2;
            case 4:
                return 3;
            case 5:
                return 4;
            default:
                return super.getItemViewType(position);
        }
    }

    public View getView(int type) {
        switch (type) {
            case 0:
                return inflater.inflate(R.layout.item_profession_info_img_text, null);
            case 1:
                return inflater.inflate(R.layout.item_profession_info_text, null);
            case 2:
                return inflater.inflate(R.layout.item_profession_info_img, null);
            case 3:
                return inflater.inflate(R.layout.item_profession_info_vedio_text, null);
            case 4:
                return inflater.inflate(R.layout.item_profession_info_video, null);
        }
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = getView(getItemViewType(position));
            holder = new ViewHolder();
            initView(getItemViewType(position), convertView, holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        loadData(position, context, holder);

        return convertView;
    }

    private void initView(int type, View view, ViewHolder holder) {
        switch (type) {
            case 0: {
                holder.professionInfoImgTextTitle = (TextView) view.findViewById(R.id.profession_info_img_text_title);
                holder.professionInfoImgTextImg = (ImageView) view.findViewById(R.id.profession_info_img_text_img);
                holder.professionInfoImgTextContent = (TextView) view.findViewById(R.id.profession_info_img_text_content);
                holder.professionInfoImgTextTime = (TextView) view.findViewById(R.id.profession_info_img_text_time);
                break;
            }
            case 1: {
                holder.professionInfoTextTitle = (TextView) view.findViewById(R.id.profession_info_text_title);
                holder.professionInfoTextContent = (TextView) view.findViewById(R.id.profession_info_text_content);
                holder.professionInfoTextTime = (TextView) view.findViewById(R.id.profession_info_text_time);
                break;
            }
            case 2: {
                holder.professionInfoImgTitle = (TextView) view.findViewById(R.id.profession_info_img_title);
                holder.professionInfoImgTime = (TextView) view.findViewById(R.id.profession_info_img_time);
                holder.professionInfoImgImg1 = (ImageView) view.findViewById(R.id.profession_info_img_img1);
                holder.professionInfoImgImg2 = (ImageView) view.findViewById(R.id.profession_info_img_img2);
                holder.professionInfoImgImg3 = (ImageView) view.findViewById(R.id.profession_info_img_img3);
                break;
            }
            case 3: {
                holder.professionInfoVedioTextImg = (ImageView) view.findViewById(R.id.profession_info_vedio_text_img);
                holder.professionInfoVedioTextTitle = (TextView) view.findViewById(R.id.profession_info_vedio_text_title);
                holder.professionInfoVedioTextText = (TextView) view.findViewById(R.id.profession_info_vedio_text_content);
                holder.professionInfoVedioTextTime = (TextView) view.findViewById(R.id.profession_info_vedio_text_time);
            }
            case 4: {
                holder.professionInfoVideoTitle = (TextView) view.findViewById(R.id.profession_info_video_title);
                holder.professionInfoVideoImg = (ImageView) view.findViewById(R.id.profession_info_video_img);
                holder.professionInfoVideoTime = (TextView) view.findViewById(R.id.profession_info_video_time);
                break;
            }
        }
    }

    public void loadData(int position, Context context, ViewHolder holder) {
        switch (getItemViewType(position)) {
            case 0: {
                holder.professionInfoImgTextTitle.setText(list.get(position).getTitle());
                holder.professionInfoImgTextContent.setText(list.get(position).getContent());
                holder.professionInfoImgTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline()))));
                String img = list.get(position).getImg().get(0);
                if (!(img == null || img.isEmpty())) {
                    Glide.with(context).load(list.get(position).getImg().get(0)).into(holder.professionInfoImgTextImg);
                }
                break;
            }
            case 1: {
                holder.professionInfoTextTitle.setText(list.get(position).getTitle());
                holder.professionInfoTextContent.setText(list.get(position).getContent());
                holder.professionInfoTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline()))));
                break;
            }
            case 2: {
                holder.professionInfoImgTitle.setText(list.get(position).getTitle());
                holder.professionInfoImgTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline()))));

                /**
                 * 可能多图的资讯没有固定的三张图片.
                 */
                String img1Path = list.get(position).getImg().size() >= 1 ? list.get(position).getImg().get(0): null;
                String img2Path = list.get(position).getImg().size() >= 2 ? list.get(position).getImg().get(1): null;
                String img3Path = list.get(position).getImg().size() >= 3 ? list.get(position).getImg().get(2): null;

                if (!(img1Path == null || img1Path.isEmpty())) {
                    Glide.with(context).load(list.get(position).getImg().get(0)).into(holder.professionInfoImgImg1);
                }
                if (!(img2Path == null) || img2Path.isEmpty()) {
                    Glide.with(context).load(list.get(position).getImg().get(1)).into(holder.professionInfoImgImg2);
                }
                if (!(img3Path == null) || img3Path.isEmpty()) {
                    Glide.with(context).load(list.get(position).getImg().get(2)).into(holder.professionInfoImgImg3);
                }
                break;
            }
            case 3: {
                holder.professionInfoVedioTextTitle.setText(list.get(position).getTitle());
                holder.professionInfoVedioTextText.setText(list.get(position).getContent());
                holder.professionInfoVedioTextTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline()))));
                String img = list.get(position).getImg().get(0);
                if (!(img == null || img.isEmpty())) {
                    Glide.with(context).load(list.get(position).getImg().get(0)).into(holder.professionInfoVedioTextImg);
                }
                break;
            }
            case 4: {
                holder.professionInfoVideoTitle.setText(list.get(position).getTitle());
                holder.professionInfoVideoTime.setText(DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline()))));
                String img = list.get(position).getImg().get(0);
                if (!(img.isEmpty() || img == null)) {
                    Glide.with(context).load(list.get(position).getImg().get(0)).into(holder.professionInfoVideoImg);
                }
                break;
            }
        }
    }




    static class ViewHolder {

        // 1
        ImageView professionInfoImgTextImg;
        TextView professionInfoImgTextTitle;
        TextView professionInfoImgTextContent;
        TextView professionInfoImgTextTime;

        //2
        TextView professionInfoTextTitle;
        TextView professionInfoTextContent;
        TextView professionInfoTextTime;

        //3
        TextView professionInfoImgTitle;
        TextView professionInfoImgTime;
        ImageView professionInfoImgImg1;
        ImageView professionInfoImgImg2;
        ImageView professionInfoImgImg3;

        //4
        ImageView professionInfoVedioTextImg;
        TextView professionInfoVedioTextTitle;
        TextView professionInfoVedioTextText;
        TextView professionInfoVedioTextTime;

        //5
        TextView professionInfoVideoTitle;
        TextView professionInfoVideoTime;
        ImageView professionInfoVideoImg;
    }
}